#include "holoserver.h"


HoloServer::HoloServer()
{
    init_gui();
    timecal = millis()-5000;
    if (WiFi.getMode()==WIFI_STA&&WiFi.status()==WL_CONNECTED)
    {
        ssid = WiFi.SSID();
        ip = WiFi.localIP().toString();
        is_connected = true;
    }
    else if(WiFi.getMode()==WIFI_AP)
    {
        ssid = WiFi.softAPSSID();
        ip = WiFi.softAPIP().toString();
        is_connected = true;
    }
    else
    {
        ssid = "T_T";
        ip = "0.0.0.0";
        is_connected = false;
    }
    String user_name = "holopoki", pw = "poki123";
    show_msg(LV_SYMBOL_FILE, ssid, ip, "User Name: " + user_name, "Password: " + pw);
    save_cfg.begin("Config");
    if (is_connected)
    {
        server.begin(user_name, pw);
    }
}

void HoloServer::init_gui()
{
    label_scr = lv_obj_create(NULL, NULL);
    lv_obj_set_style_local_bg_color(label_scr, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
	lv_scr_load(label_scr);

    lv_style_init(&font_style);
	lv_style_set_text_font(&font_style, LV_STATE_DEFAULT, &lv_font_montserrat_34);
	lv_style_set_text_color(&font_style, LV_STATE_DEFAULT, LV_COLOR_YELLOW);

    label_ip = lv_label_create(label_scr, NULL);
    label_sym = lv_label_create(label_scr, NULL);
    label_ssid = lv_label_create(label_scr, NULL);
    label_user_name = lv_label_create(label_scr, NULL);
    label_pw = lv_label_create(label_scr, NULL);
    
	lv_obj_add_style(label_ip, LV_LABEL_PART_MAIN, &font_style);
    lv_obj_add_style(label_sym, LV_LABEL_PART_MAIN, &font_style);
    lv_obj_add_style(label_ssid, LV_LABEL_PART_MAIN, &font_style);
    lv_obj_add_style(label_user_name, LV_LABEL_PART_MAIN, &font_style);
    lv_obj_add_style(label_pw, LV_LABEL_PART_MAIN, &font_style);
}

Event HoloServer::main_process(Action act)
{
    event = NOEVENT;
    server.handleFTP();
    lv_task_handler();
    return event;
}

void HoloServer::show_msg(String sym, String ssid, String ip, String user_name, String pw)
{

    lv_label_set_text(label_user_name, "User Name:    ");
    lv_label_set_text(label_pw, "Password:       ");
    lv_label_set_long_mode(label_user_name, LV_LABEL_LONG_SROLL);
    lv_label_set_long_mode(label_pw, LV_LABEL_LONG_SROLL);
    lv_label_set_text(label_sym, sym.c_str());
    lv_label_set_text(label_ssid, ssid.c_str());
    lv_label_set_text(label_ip, ip.c_str());
    lv_label_set_text(label_user_name, user_name.c_str());
    lv_label_set_text(label_pw, pw.c_str());

    lv_obj_align(label_sym, NULL, LV_ALIGN_CENTER, 0, -100);
    lv_obj_align(label_ssid, NULL, LV_ALIGN_CENTER, 0, -50);
    lv_obj_align(label_ip, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_align(label_user_name, NULL, LV_ALIGN_CENTER, 0, 50);
    lv_obj_align(label_pw, NULL, LV_ALIGN_CENTER, 0, 100);
    lv_task_handler();
}

HoloServer::~HoloServer()
{
    lv_obj_del(label_scr);
}